export var webviewSystem = function () {
  const u = navigator.userAgent;
  const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
  const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  const ua = window.navigator.userAgent.toLowerCase();

  if (ua.match(/MicroMessenger/i) == 'micromessenger') {
    console.log('微信');
    return 'wx';
  }
  console.log('非微信');

  if (isiOS) {
    console.log('iOS');
    return 'iOS';
  }
  if (isAndroid) {
    console.log('isAndroid');
    return 'Android';
  }
  console.log('非微信web', ua);

  return 'web';
};
const browser = webviewSystem();

/* **
 * iSO Bridge
 * connectWebViewJavascriptBridge
 * ***/
function connectWebViewJavascriptBridge(callback) {

  console.log('init', 222)
  console.log('initwindow', window)
  console.log('init', window.WVJBCallbacks)

  if (window.WebViewJavascriptBridge) {
    console.log('init', 333)
    console.log('init', window.WebViewJavascriptBridge)

    callback(WebViewJavascriptBridge);
  } else {
    console.log('init', 444)
    // console.log('WebViewJavascriptBridge',WebViewJavascriptBridge)
    // console.log('window', window)

    // document.addEventListener('WebViewJavascriptBridgeReady', function() {
    console.log('init', 5555)

    // callback(WebViewJavascriptBridge);
    // }, false);
  }


}


window.sendDataToOC = function (params) {

};
console.log('system', browser);

function setupWebViewJavascriptBridge(callback) {
  setTimeout(() => {
    try {
      console.log('set1')
      console.log('window',window);
      console.log('window.WVJBCallbacks',window.WVJBCallbacks);
      // console.log('WVJBCallbacks', WVJBCallbacks);
      // console.log('window.WVJBCallbacks()',window.WVJBCallbacks());
      // console.log('window.WVJBCallbacks[0]',window.WVJBCallbacks[0]);
      console.log('window.Reflect',window.Reflect);

      if (window.WebViewJavascriptBridge) {
        console.log('set2')
        return callback(WebViewJavascriptBridge);
      }
      if (window.WVJBCallbacks) {
        console.log('set3')
        return window.WVJBCallbacks.push(callback);
      }
      console.log('set4')
      window.WVJBCallbacks = [callback];
      var WVJBIframe = document.createElement('iframe');
      WVJBIframe.style.display = 'none';
      WVJBIframe.src = 'https://__bridge_loaded__';
      document.documentElement.appendChild(WVJBIframe);
      setTimeout(function () {
        document.documentElement.removeChild(WVJBIframe)
      }, 0)
    } catch (e) {
      console.log(e)
    }
  }, 2000)
}


// connectWebViewJavascriptBridge(function (bridge) {
//   //初始化
//   console.log('init', 111)
//   bridge.init(function (message, responseCallback) {
//     var data = {
//       'Javascript Responds': 'Wee!'
//     };
//     alert("jasdashjd");
//     responseCallback(data);
//   });
//
//   bridge.registerHandler("functionInJs", function (data, responseCallback) {
//     alert(data);
//     var data2 = document.getElementById("text1").value;
//     var responseData = "我是Android调用js方法返回的数据---" + data2;
//     responseCallback(responseData);
//   });
// })

setupWebViewJavascriptBridge(function (bridge) {
  /* Initialize your app here */
  // bridge.registerHandler('JS Echo', function(data, responseCallback) {
  //   console.log('JS Echo called with:', data)
  //   responseCallback(data)
  // })
  // bridge.callHandler('ObjC Echo', {'key': 'value'}, function responseCallback(responseData) {
  //   console.log('JS received response:', responseData)
  // });
  console.log('进入setupWebViewJavascriptBridge')
  console.log('window',window)
  console.log('bridge',bridge)
  console.log('window.sendDataToOc', window.sendDataToOC)

  window.sendDataToOC = function (params) {
    console.log('params', params)
    console.log('browser', browser)

    if (browser == 'iOS') {
      bridge.callHandler('javascriptHandler', params, function (response) {
        if (typeof response.uid == 'undefined') {
          return;
        }
        loginSite(response);
      });
    }

    //Android调用js方法：functionInJs方法名称需要保持一致 ，并返回给Android通知

    // bridge.registerHandler(params.action_name, function (data, responseCallback) {
    //   alert('hahhah');
    //   // var data2 = document.getElementById("text1").value;
    //   // var responseData = "我是Android调用js方法返回的数据---"+ data2;
    //   console.log('老子成功了。哈哈哈哈哈2222', responseCallback)
    //
    //   responseCallback('responseData');
    // });

    // 方法没有调用，安卓执行不到这里
    if (browser == 'Android') { // android
      console.log('Android1111111')
      window.WebViewJavascriptBridge.callHandler(params.action_name, params.params, function (responseData) {
        console.log('WebViewJavascriptBridge.callHandler',1111)
        console.log('responseData',responseData)
          // document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData
        }
      );
      console.log('Android22222')
      window.WVJBCallbacks.callHandler(params.action_name, params.params, function (responseData) {
          console.log('WVJBCallbacks.callHandler',1111)
        console.log('responseData',responseData)
          // document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData
        }
      );
      console.log('Android3333')
      window.WVJBCallbacks(params.action_name, params.params, function (responseData) {
          console.log('WVJBCallbacks',1111)
          console.log('responseData',responseData)
          // document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData
        }
      );
    }
  }
})
// if (browser == 'iOS') {
//   connectWebViewJavascriptBridge(
//     function(bridge) {
//       bridge.init(function(message, responseCallback) {
//       });
//
//       bridge.registerHandler('javascriptHandler', function(data, responseCallback) {
//       });
//
//       window.sendDataToOC = function(params) {
//         bridge.callHandler('javascriptHandler', params, function(response) {
//           if (typeof response.uid == 'undefined') {
//             return;
//           }
//           loginSite(response);
//         });
//       };
//     }
//   );
// } else if (browser == 'Android') { //android
//   window.sendDataToOC = function(params) {
//     let a = '';
//     a = JSON.stringify(params);
//     console.log('Android,toc');
//     window.javascriptHandler.sendDataToOC(a);
//   };
// } else if (browser == 'wx') {
// }
//
// // 专家版 使用 wkview 注册方法
// function sendDataToExp(params) {
//   if (browser == 'iOS') {
//     window.webkit.messageHandlers.MModel.postMessage(params);
//   } else {
//     let a = '';
//     a = JSON.stringify(params);
//     window.javascriptHandler.sendDataToOC(a);
//   }
// }
function sendData(par, action_name, scheme, type) {
  const params = {};
  params.cmd = {
    params: par,
    action_name
  };
  params.url = scheme;

  console.log(params);
  window.sendDataToOC(params);
}

// // 组装传入参数
// function setParams(actionName, params, _url) {
//   const data = {
//     url: _url,
//     cmd: {
//       action_name: actionName,
//       params
//     }
//   };
//   console.log(data);
//   sendDataToOC(data);
// }

// 组装传入参数
function setParamsRouter(_url) {
  console.log('setParamsRouter', _url);
  window.sendDataToOC(_url);
}

function setParams(actionName, params, _url) {
  const data = {
    // url: _url,
    cmd: {
      action_name: actionName,
      params
    }
  };
  console.log(data);

  window.sendDataToOC(data);
}

/* **
 * native方法
 *
 * ***/

export {
  setParams,
  setParamsRouter
};
